package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.constant.MessageConstant;
import com.mfl.context.BaseContext;
import com.mfl.exception.TestExamAlreadyExistsException;
import com.mfl.mapper.TestCategoryMapper;
import com.mfl.mapper.TestExamMapper;
import com.mfl.mapper.TestExamTopicMapper;
import com.mfl.pojo.dto.TestExamDTO;
import com.mfl.pojo.dto.TestExamPageQueryDTO;
import com.mfl.pojo.entity.TestCategory;
import com.mfl.pojo.entity.TestExam;
import com.mfl.pojo.entity.TestExamTopic;
import com.mfl.pojo.vo.TestExamPageQueryVO;
import com.mfl.service.TestExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class TestExamServiceImpl implements TestExamService {

    @Autowired
    private TestCategoryMapper testCategoryMapper;
    @Autowired
    private TestExamMapper testExamMapper;
    @Autowired
    private TestExamTopicMapper testExamTopicMapper;

    /**
     * 分页查询测试试卷
     * @param testExamPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(TestExamPageQueryDTO testExamPageQueryDTO) {
        PageHelper.startPage(testExamPageQueryDTO.getPage(), testExamPageQueryDTO.getPageSize());
        Page<TestExamPageQueryVO> page = testExamMapper.pageQuery(testExamPageQueryDTO);
        page.getResult().forEach(item -> {
            // 根据测试试卷id在test_exam_topic表中查询出对应的题目信息
            List<TestExamTopic> testExamTopicList = testExamTopicMapper.selectByTestExamId(item.getId());
            AtomicInteger score = new AtomicInteger();
            if(testExamTopicList.size() > 0){
                item.setCount(testExamTopicList.size());
                testExamTopicList.forEach(testExamTopic -> {
                    score.addAndGet(testExamTopic.getTopicScore());});
            }
            item.setScore(score.get());
            // 查询测试试卷对应的测试时长
            TestCategory testCategory = testCategoryMapper.getByTestCategoryId(item.getTestCategoryId());
            item.setDuration(testCategory.getTestTime());
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增测试试卷
     * @param testExamDTO
     */
    @Override
    public void insert(TestExamDTO testExamDTO) {
       // 查询测试卷名是否存在
        TestExam testExam = testExamMapper.getByTestExamName(testExamDTO.getExamName());
        if (testExam != null) {
            throw new TestExamAlreadyExistsException(MessageConstant.TEST_EXAM_ALREADY_EXISTS);
        }
        TestExam build = TestExam.builder()
                .examName(testExamDTO.getExamName())
                .testCategoryId(testExamDTO.getTestCategoryId())
                .createTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .build();
        testExamMapper.insert(build);
    }

    /**
     * 批量删除测试试卷
     * @param ids
     */
    @Transactional
    @Override
    public void batchDelete(List<Integer> ids) {
        testExamMapper.deleteByIds(ids);
        // 删除试卷对应的题目
        testExamTopicMapper.deleteByTestExamIds(ids);
    }
}
